package com.cpecc.dao.wuzi;

import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.cpecc.model.wuzi.BaoFei;
import com.cpecc.model.wuzi.JiYao;

@Repository
public class JiYaoDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void save(JiYao jiYao){
		Session session=sessionFactory.getCurrentSession();
		session.save(jiYao);
		session.flush();
	}
	
	public boolean cover(JiYao jiYao){
		String hql="update JiYao t set t.baoFei=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, jiYao.getBaoFei());
		query.setString(1, jiYao.getId());
		return query.executeUpdate()>0;
	}
	
	public JiYao get(String id){
		return (JiYao) sessionFactory.getCurrentSession().get(JiYao.class, id);
	}
	
	public boolean delete(String id){
		String hql="delete JiYao t where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);
		return query.executeUpdate()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<JiYao> list(BaoFei baoFei){
		String hql="from JiYao t where t.baoFei=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, baoFei);
		return query.list();
	}
}
